package com.it.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.it.constant.MessageConstant;
import com.it.entity.PageResult;
import com.it.entity.QueryPageBean;
import com.it.entity.Result;
import com.it.pojo.CheckGroup;
import com.it.service.CheckGroupService;
import org.apache.xmlbeans.ResourceLoader;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */

@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        System.out.println("添加检查组");
        try{
//            int a = 1 / 0;
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            System.out.println("添加检查组异常"+e);
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);

        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer curentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        System.out.println("检查组分页查询currentPage:%d pageSize:%d");


        PageResult pageResult= checkGroupService.pageQuery(queryPageBean);
        return pageResult;

    }

    //根据id查询检查组信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
//        CheckGroup checkGroup = checkGroupService.findById(id);
        if (checkGroup != null){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }else{
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //根据检查组id查询检查项
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        if (checkitemIds != null){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }else{
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try{
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            System.out.println("编辑检查组异常"+e);
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);

        }

    }

    //检查组查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        if (checkGroups != null & checkGroups.size() >0){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);

        }else{
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }

    }

    //根据检查组id删除检查组
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            checkGroupService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);

        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }


}
